//
//  ViewController.swift
//  TestView
//
//  Created by apple on 16/12/28.
//  Copyright © 2016年 com.zaoguankeji.www. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        testExpandButtonClickArea()
//       testOverSuperview()
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    func testOverSuperview(){
        
        let view1 = TestView(frame:CGRect(x: 100, y: 100, width: 200, height: 200))
        view1.backgroundColor = UIColor.lightGray
        view.addSubview(view1)
        
        let view2 = UIView(frame: CGRect(x: 40, y: 40, width: 100, height: 100))
        view2.backgroundColor = .red
        view1.addSubview(view2)
        
        let view3 = UIButton(type: .custom)
        view3.frame = (frame: CGRect(x: 10, y: 10, width: 200, height: 80))
        view3.backgroundColor = .purple
        view3.addTarget(self, action: #selector(ViewController.tapButton), for: .touchUpInside)
        view2.addSubview(view3)
    }
    
    func tapButton(){
        print("button has been pressed!");
    }
    
    func testExpandButtonClickArea(){
        
        //为了便于观察，添加一个背景视图，大小正好为100*100
        let backgroundView = UIView(frame:  CGRect(x: 60, y: 160, width: 100, height: 100))
        backgroundView.backgroundColor = .purple
        view.addSubview(backgroundView)
        
        let btn = MyButton(type: .custom)
        btn.frame = CGRect(x: 100, y: 200, width: 20, height: 20)
        btn.backgroundColor = .red
        btn.setTitle("btn", for: .normal)
        btn.addTarget(self, action: #selector(ViewController.tapButtonAction), for: .touchUpInside)
        view.addSubview(btn)
    }
    
    func tapButtonAction(){
        print("button has been pressed!");
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

class MyButton: UIButton {
    override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        if !self.isUserInteractionEnabled || self.isHidden || self.alpha == 0 {
            return nil
        }
        
        if self.bounds.insetBy(dx: -40, dy: -40).contains(point){
            for subview in self.subviews.reversed() {
                let convertPoint = subview.convert(point, from:self)
                if let sview = subview.hitTest(convertPoint, with: event) {
                    return sview
                }
            }
            return self
        }
        return nil
    }
}

class TestView: UIView {
    override open func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
        super.hitTest(point, with: event)
        return overlapHitTest(point, withEvent: event)
    }
}

extension UIView {
    fileprivate func overlapHitTest(_ point: CGPoint, withEvent event: UIEvent?) -> UIView? {
        // We should not send touch events for hidden or transparent views, or views with userInteractionEnabled set to NO;
        if !self.isUserInteractionEnabled || self.isHidden || self.alpha == 0 {
            return nil
        }
        // If touch is inside self, self will be considered as potential result.
        var hitView: UIView? = self
        if !self.point(inside: point, with: event) {
            if self.clipsToBounds {
                return nil
            } else {
                hitView = nil
            }
        }
        // Check recursively all subviews for hit. If any, return it.
        for subview in self.subviews.reversed() {
            let insideSubview = self.convert(point, to: subview)
            if let sview = subview.overlapHitTest(insideSubview, withEvent: event) {
                return sview
            }
        }
        // Else return self or nil depending on result from step 2.
        return hitView as! UIButton?
    }
    
}

